package com.tl.khadmin.service.impl;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import com.tl.khadmin.bean.DeliveryAddress;
import com.tl.khadmin.bean.Staff;
import com.tl.khadmin.bean.Supplier;
import com.tl.khadmin.mapper.DeliveryAddressMapper;  
import com.tl.khadmin.page.Page;
import com.tl.khadmin.service.DeliveryAddressIService;  
import com.tl.khadmin.util.CommonUtil;
import com.tl.khadmin.util.Constant; 

@Service
public class DeliveryAddressServiceImpl implements  DeliveryAddressIService{
	
	@Resource
	private DeliveryAddressMapper deliveryAddressMapper;
	
	public List<DeliveryAddress> listDeliveryAddress(HttpSession session, Map<String, Object> param){ 
		Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER);
		param.put("supplierId", supplier.getId());
		return deliveryAddressMapper.listDeliveryAddress(param); 
	}
	
	@Override
	public String updateDeliveryAddress(DeliveryAddress deliveryAddress) { 
		int result = deliveryAddressMapper.updateByPrimaryKeySelective(deliveryAddress);
		return CommonUtil.returnMapResultByRows(result, "修改成功", "修改失败，请重试");
	}
	
	@Override
	public Page<DeliveryAddress> showDeliveryAddressByPage(HttpSession session, Integer pageNum, 
			Integer pageSize, Map<String, Object> params) {
		Page<DeliveryAddress> page=new Page<DeliveryAddress>(pageNum, pageSize);
		Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER);
		params.put("supplierId", supplier.getId());
        params.put("page", page);   
        List<DeliveryAddress> list= deliveryAddressMapper.showDeliveryAddressByPage(params);  
		page.setResult(list);
		return page;
	}

	@Override
	public String saveDeliveryAddress(HttpSession session, DeliveryAddress deliveryAddress) { 
		Staff staff = (Staff) session.getAttribute(Constant.USER);
		Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER);
		deliveryAddress.setCreateTime(new Date()); 
		deliveryAddress.setCreateId(staff.getId());
		deliveryAddress.setSupplierId(supplier.getId());
		int result = deliveryAddressMapper.insertSelective(deliveryAddress);
		return CommonUtil.returnMapResultByRows(result, "添加成功", "添加失败，请重试");
	} 
	
	@Override
	public String deleteByIds(String ids) {
		String[] idsArr = ids.substring(0,ids.length()-1).split(","); 
		try{
			if(deliveryAddressMapper.deleteByIds(idsArr) > 0) {
				return CommonUtil.returnMapResultByRows(1, "删除成功","删除失败，请重试");
			} 
		}catch(Exception ex) {
			return CommonUtil.returnMapResultByRows(0, "删除成功","删除失败，请重试");
		} 
		return CommonUtil.returnMapResultByRows(0, "删除成功","删除失败，请重试");
	}

	@Override
	public Map<String, Object> queryDeliveryAddressById(Integer id) {
		Map<String, Object> result = new HashMap<>();
		DeliveryAddress DeliveryAddress = deliveryAddressMapper.selectByPrimaryKey(id); 
		result.put("DeliveryAddress", DeliveryAddress); 
		return result;
	}

	@Override
	public String updateStatus(DeliveryAddress deliveryAddress) {
		int result = deliveryAddressMapper.updateByPrimaryKeySelective(deliveryAddress);
		return CommonUtil.returnMapResultByRows(result, "修改成功", "修改失败，请重试");
	} 
}
